<template>
  <public-modal
    width="800px"
    :footer="false"
    title="重新设定密码"
    modalStyle="PasswordModal"
    :publicVisible="publicVisible"
    @handleOk="handleOk"
    @cancel="cancel"
  >
    <div slot="content">
      <a-form :form="form" class="formStyle">
        <a-form-item label="用户账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input :maxLength="30" placeholder="请输入用户账号" v-decorator="['username', {}]" :readOnly="true" />
        </a-form-item>
        <a-form-item label="登录密码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            :maxLength="16"
            type="password"
            placeholder="请输入新的登录密码"
            v-decorator="['password', validatorRules.password]"
          />
        </a-form-item>
        <a-form-item label="确认密码" :labelCol="labelCol" :wrapperCol="wrapperCol" hasFeedback>
          <a-input
            :maxLength="16"
            type="password"
            @blur="handleConfirmBlur"
            placeholder="请再次输入新的登录密码"
            v-decorator="['confirmpassword', validatorRules.confirmpassword]"
          />
        </a-form-item>
      </a-form>
    </div>
  </public-modal>
</template>

<script>
import { changePassword } from '@/api/api'
import publicModal from '@/components/publicModal'
export default {
  name: 'PasswordModal',
  components: { publicModal },
  data() {
    return {
      publicVisible: false,
      confirmLoading: false,
      confirmDirty: false,
      validatorRules: {
        password: {
          rules: [
            {
              required: true,
              message: '请输入密码!',
            },
            {
              // ^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$
              // pattern: /^(?=.*?[0-9])[0-9A-Za-z~!@#￥%&*.]{8,16}$/,
              pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z~!@#￥%&*.]{8,16}$/,
              message: '密码必须由8-16位数字、大小写字母组成，可有~!@#￥%&*.等特殊字符!',
            },
            {
              validator: this.validateToNextPassword,
            },
          ],
        },
        confirmpassword: {
          rules: [
            {
              required: true,
              message: '请重新输入登录密码!',
            },
            {
              validator: this.compareToFirstPassword,
            },
          ],
        },
      },
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      form: this.$form.createForm(this),
    }
  },

  methods: {
    show(username) {
      this.form.resetFields()
      this.publicVisible = true
      this.model.username = username
      this.$nextTick(() => {
        this.form.setFieldsValue({ username: username })
      })
    },
    cancel() {
      this.publicVisible = false
      this.disableSubmit = false
      this.selectedRole = []
    },
    handleOk() {
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          let formData = Object.assign(this.model, values)
          changePassword(formData)
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.message)
                this.$emit('refresh')
              } else {
                this.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.cancel()
            })
        }
      })
    },
    handleCancel() {
      this.cancel()
    },
    validateToNextPassword(rule, value, callback) {
      const form = this.form
      const confirmpassword = form.getFieldValue('confirmpassword')
      console.log('confirmpassword==>', confirmpassword)
      if (value && confirmpassword && value !== confirmpassword) {
        callback('两次输入的密码不一样！')
      }
      if (value && this.confirmDirty) {
        form.validateFields(['confirm'], { force: true })
      }
      callback()
    },
    compareToFirstPassword(rule, value, callback) {
      const form = this.form
      if (value && value !== form.getFieldValue('password')) {
        callback('两次输入的密码不一样！')
      } else {
        callback()
      }
    },
    handleConfirmBlur(e) {
      const value = e.target.value
      this.confirmDirty = this.confirmDirty || !!value
    },
  },
}
</script>
